package com.eattravel.controller;

import com.eattravel.dto.PageInfoResult;
import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.Order;
import com.eattravel.pojo.OrderCommodity;
import com.eattravel.service.OrderService;
import com.eattravel.service.ShoppingCartService;
import com.eattravel.service.impl.ShoppingCartServiceImpl;
import com.eattravel.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/shoppingcart")
public class ShoppingCartConntroller {

    @Autowired
    ShoppingCartService shoppingCartService;

    @ResponseBody
    @RequestMapping("/showcommodity")
    public List<Commodity> selectAllCommodityCollection(String userId) {
        List<Commodity> commodityList = shoppingCartService.selectShoppingCart(userId);
        return commodityList;
    }

    @ResponseBody
    @RequestMapping("/deletecommcdity")
    public ResultData deleteCommodity(String userId, String commodityId) {
        ResultData resultData = new ResultData();

        int rs = shoppingCartService.deleteCommodity(userId, commodityId);
        if (rs > 0) {
            resultData.setCode(0);
            resultData.setMessage("删除成功");

        } else {
            resultData.setCode(1);
            resultData.setMessage("删除失败");

        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping("/addtocoll")
    public ResultData addToColl(String userId, String commodityId) {
        ResultData resultData = new ResultData();

        int rs = shoppingCartService.addToColl(userId, commodityId);
        if (rs == 1) {
            resultData.setCode(1);
            resultData.setMessage("收藏中已经有了这个商品");

        } else {
            resultData.setCode(0);
            resultData.setMessage("添加收藏成功");
        }
        return resultData;
    }

    @Autowired
    OrderService orderService;

    @ResponseBody
    @RequestMapping("/addorder")
    public ResultData addOrder(String userId, String[] commodityIds, int[] commodityNums) {
        String[] commodityIds2 = new String[commodityIds.length];
        for (int i = 0; i < commodityIds.length; i++) {
            commodityIds2[i] = commodityIds[i].substring(0, 6);
        }
        Set set = new HashSet();
        for (int i = 0; i < commodityIds2.length; i++) {
            set.add(commodityIds2[i]);
        }
        //商家的数量
        int orderNum = set.size();
        //商品的商家id
        String[] storeId = new String[orderNum];
        Iterator it = set.iterator();
        int index = 0;
        while (it.hasNext()) {
            storeId[index] = (String) it.next();
            index++;
        }
        Date date = new Date();
        int rs = 0;
        for (int i = 0; i < orderNum; i++) {
            Order order = new Order();
            String orderId = (UUID.randomUUID().toString()).substring(0, 20);
            order.setOrderId(orderId);
            order.setStoreId(storeId[i]);
            order.setOrderHours(date);
            //先向订单中添加数据
            rs = orderService.insertIntoOrder(order, userId);
            for (int j = 0; j < commodityIds.length; j++) {
                if (commodityIds[j].substring(0, 6).equals(storeId[i])) {
                    //在向订单商品中添加数据
                    int rs2 = orderService.insertCommodity(orderId, commodityIds[j], commodityNums[j]);
                }
            }
        }
        ResultData resultData = new ResultData();
        if (rs == 1) {
            resultData.setCode(1);
            resultData.setMessage("已经添加到订单");

        } else {
            resultData.setCode(0);
            resultData.setMessage("结算失败");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping("/updatecommnum")
    public void updateCommNum(String userId, String commodityId, int num) {
        int rs = shoppingCartService.updateCommodityNum(userId, commodityId, num);
    }
}
